package com.violet.excel.utils;

import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.stereotype.Component;

import java.io.FileOutputStream;
import java.util.List;

@Component
public class ExcelUtils {
    /**
     * 导出多页 Excel
     *
     * @param sheetList 每个 sheet 的数据列表
     * @param sheetNameList 每个 sheet 的名称列表
     * @param filePath 导出文件的路径
     * @throws Exception
     */
    public void exportMultiSheetExcel(List<List<?>> sheetList, List<String> sheetNameList, String filePath) throws Exception {
        // 创建工作簿
        Workbook workbook = new SXSSFWorkbook(1000);
        // 遍历每个 sheet
        for (int i = 0; i < sheetList.size(); i++) {
            List<?> dataList = sheetList.get(i);
            String sheetName = sheetNameList.get(i);
            // 创建 sheet
            Sheet sheet = workbook.createSheet(sheetName == null ? "Sheet" + (i + 1) : sheetName);
            // 写入表头
            writeHeader(sheet);
            // 写入数据
            writeData(sheet, dataList);
        }
        // 输出到文件
        FileOutputStream fos = new FileOutputStream(filePath);
        workbook.write(fos);
        fos.close();
        workbook.close();
    }
    /**
     * 写入表头
     *
     * @param sheet
     */
    private void writeHeader(Sheet sheet) {
        // TODO: 这里写入表头的逻辑
    }
    /**
     * 写入数据
     *
     * @param sheet
     * @param dataList
     */
    private void writeData(Sheet sheet, List<?> dataList) {
        // TODO: 这里写入数据的逻辑
    }
}